package com.example;

import com.example.util.CommonUtils;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class TaskService {

    public static void main(String[] args) {
        TaskService service = new TaskService();
        service.executeAsyncNonBlocking();

        CommonUtils.sleepSecond(5);
    }

    public void executeAsyncNonBlocking() {
        CommonUtils.printThreadLog("----------执行异步任务（非阻塞），开始----------");

        long start = System.currentTimeMillis();
        CompletableFuture<Void> task1Future = CompletableFuture.runAsync(() -> {
            task1();
        });

        CompletableFuture<Void> task2Future = CompletableFuture.runAsync(() -> {
            task2();
        });

        CompletableFuture<Void> task3Future = CompletableFuture.runAsync(() -> {
            task3();
        });

        CompletableFuture.allOf(task1Future, task2Future, task3Future).thenRun(() -> {
            CommonUtils.printThreadLog("----------执行所有异步任务，结束----------");
            CommonUtils.printThreadLog("执行时间：" + (System.currentTimeMillis() - start) + " 毫秒");
        });

        CommonUtils.printThreadLog("----------执行异步任务（主线程），结束----------");
        CommonUtils.printThreadLog("执行时间：" + (System.currentTimeMillis() - start) + " 毫秒");
    }

    /**
     * 执行异步任务
     *
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public void executeAsync() throws InterruptedException, ExecutionException {
        CommonUtils.printThreadLog("----------执行异步任务，开始----------");

        long start = System.currentTimeMillis();
        CompletableFuture<Void> task1Future = CompletableFuture.runAsync(() -> {
            task1();
        });

        CompletableFuture<Void> task2Future = CompletableFuture.runAsync(() -> {
            task2();
        });

        CompletableFuture<Void> task3Future = CompletableFuture.runAsync(() -> {
            task3();
        });

        CompletableFuture.allOf(task1Future, task2Future, task3Future).get();

        long end = System.currentTimeMillis();

        CommonUtils.printThreadLog("----------执行异步任务，结束----------");
        CommonUtils.printThreadLog("执行时间：" + (end - start) + " 毫秒");
    }

    /**
     * 执行同步任务
     */
    public void executeSync() {
        CommonUtils.printThreadLog("----------执行同步任务，开始----------");

        long start = System.currentTimeMillis();
        task1();
        task2();
        task3();
        long end = System.currentTimeMillis();

        CommonUtils.printThreadLog("----------执行同步任务，结束----------");
        CommonUtils.printThreadLog("执行时间：" + (end - start) + " 毫秒");
    }

    private void task1() {
        try {
            CommonUtils.printThreadLog("task1 开始");
            long start = System.currentTimeMillis();
            Thread.sleep(1000);
            long end = System.currentTimeMillis();
            CommonUtils.printThreadLog("task1 结束，执行时间：" + (end - start) + " 毫秒");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    private void task2() {
        try {
            CommonUtils.printThreadLog("task2 开始");
            long start = System.currentTimeMillis();
            Thread.sleep(2000);
            long end = System.currentTimeMillis();
            CommonUtils.printThreadLog("task2 结束，执行时间：" + (end - start) + " 毫秒");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    private void task3() {
        try {
            CommonUtils.printThreadLog("task3 开始");
            long start = System.currentTimeMillis();
            Thread.sleep(3000);
            long end = System.currentTimeMillis();
            CommonUtils.printThreadLog("task3 结束，执行时间：" + (end - start) + " 毫秒");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

}

